package cn.net.qinling.healthcare.test.datagen;

import java.util.List;
import java.util.Random;

import org.openhealthtools.mdht.uml.cda.CDAFactory;
import org.openhealthtools.mdht.uml.cda.Consumable;
import org.openhealthtools.mdht.uml.cda.ManufacturedProduct;
import org.openhealthtools.mdht.uml.cda.Material;
import org.openhealthtools.mdht.uml.cda.SubstanceAdministration;
import org.openhealthtools.mdht.uml.hl7.datatypes.CE;
import org.openhealthtools.mdht.uml.hl7.datatypes.DatatypesFactory;
import org.openhealthtools.mdht.uml.hl7.datatypes.IVL_PQ;
import org.openhealthtools.mdht.uml.hl7.vocab.ActClass;
import org.openhealthtools.mdht.uml.hl7.vocab.EntityClassManufacturedMaterial;
import org.openhealthtools.mdht.uml.hl7.vocab.EntityDeterminerDetermined;
import org.openhealthtools.mdht.uml.hl7.vocab.x_DocumentSubstanceMood;

import cn.net.qinling.healthcare.CDARootIdentifiers;

public class SubstanceAdministrationGenerator extends
		Generator<SubstanceAdministration> {

	public SubstanceAdministrationGenerator(Selector selector) {
		super(selector);
	}

	@Override
	protected SubstanceAdministration doGeneration() {
		SubstanceAdministration sa = CDAFactory.eINSTANCE.createSubstanceAdministration();
		sa.setRouteCode(getRandomRouteCode());
		sa.setRateQuantity(getRandomRateQuantity());
		sa.setDoseQuantity(getRandomDoseQuantity());
		sa.setAdministrationUnitCode(getRandomAdministrationUnit());
		sa.setConsumable(getRandomConsumable());
		sa.setMoodCode(x_DocumentSubstanceMood.EVN);
		sa.setClassCode(ActClass.SBADM);
		return sa;
	}
	
	private Consumable getRandomConsumable() {
		Consumable result = CDAFactory.eINSTANCE.createConsumable();
		ManufacturedProduct product = CDAFactory.eINSTANCE.createManufacturedProduct();
		Material material = CDAFactory.eINSTANCE.createMaterial();
		material.setClassCode(EntityClassManufacturedMaterial.MMAT);
		material.setDeterminerCode(EntityDeterminerDetermined.KIND);
		material.setCode(getRandomMedicineType());
		
		product.setManufacturedMaterial(material);
		result.setManufacturedProduct(product);
		
		return result;
	}
	
	private CE getRandomMedicineType() {
		CE result = DatatypesFactory.eINSTANCE.createCE();
		List<String> selectedInfo = selector.next(CDARootIdentifiers.MEDICINE_TYPES);
		result.setCode(selectedInfo.get(0));
		result.setDisplayName(selectedInfo.get(1));
		result.setCodeSystem(CDARootIdentifiers.MEDICINE_TYPES);
		result.setCodeSystemName("国家药物目录");
		
		return result;
	}

	private CE getRandomAdministrationUnit() {
		CE result = DatatypesFactory.eINSTANCE.createCE();
		List<String> selectedInfo = selector.next(CDARootIdentifiers.MEDICINE_DOSAGEFORM);
		result.setCode(selectedInfo.get(0));
		result.setDisplayName(selectedInfo.get(1));
		result.setCodeSystem(CDARootIdentifiers.MEDICINE_DOSAGEFORM);
		result.setCodeSystemName("药物剂型代码");
		
		return result;
	}

	private IVL_PQ getRandomDoseQuantity() {
		IVL_PQ result = DatatypesFactory.eINSTANCE.createIVL_PQ();
		result.setUnit("mg");
		result.setValue(this.getRandomDoseQuantityNum());
		
		return result;
	}

	private IVL_PQ getRandomRateQuantity() {
		IVL_PQ result = DatatypesFactory.eINSTANCE.createIVL_PQ();
		result.setUnit("天");
		result.setValue(1.0);
		
		return result;
	}
	
	
	
	private CE getRandomRouteCode() {
		CE result = DatatypesFactory.eINSTANCE.createCE();
		List<String> selectedInfo = selector.next(CDARootIdentifiers.MEDICINE_USAGE);
		String id = selectedInfo.get(0);
		String name = selectedInfo.get(1);
		
		result.setCode(id);
		result.setDisplayName(name);
		result.setCodeSystem(CDARootIdentifiers.MEDICINE_USAGE);
		result.setCodeSystemName("用药途径编码");
		
		return result;
	}
	
	private double getRandomDoseQuantityNum() {
		return new Random().nextInt(20) * 5;
	}
	

}
